package com.ruoyi.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.aid.AidCategory;
import com.ruoyi.system.service.aid.IAidCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资类别Controller
 */
@RestController
@RequestMapping("/aid/category")
public class AidCategoryController extends BaseController {
    @Autowired
    private IAidCategoryService aidCategoryService;

    /**
     * 查询物资类别列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AidCategory aidCategory) {
        startPage();
        List<AidCategory> list = aidCategoryService.selectAidCategoryList(aidCategory);
        return getDataTable(list);
    }

    /**
     * 导出物资类别列表
     */
    @Log(title = "物资类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AidCategory aidCategory) {
        List<AidCategory> list = aidCategoryService.selectAidCategoryList(aidCategory);
        ExcelUtil<AidCategory> util = new ExcelUtil<AidCategory>(AidCategory.class);
        return util.exportExcel(list, "物资类别数据");
    }

    /**
     * 获取物资类别详细信息
     */
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(aidCategoryService.selectAidCategoryById(categoryId));
    }

    /**
     * 新增物资类别
     */
    @Log(title = "物资类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AidCategory aidCategory) {
        return toAjax(aidCategoryService.insertAidCategory(aidCategory));
    }

    /**
     * 修改物资类别
     */
    @Log(title = "物资类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AidCategory aidCategory) {
        return toAjax(aidCategoryService.updateAidCategory(aidCategory));
    }

    /**
     * 删除物资类别
     */
    @Log(title = "物资类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(aidCategoryService.deleteAidCategoryByIds(categoryIds));
    }

    /**
     * 获取所有物资类别（树形结构）
     */
    @GetMapping("/all")
    public AjaxResult getAllCategories() {
        List<AidCategory> list = aidCategoryService.selectAidCategoryAll();
        return success(list);
    }

    /**
     * 获取子类别
     */
    @GetMapping("/children/{parentId}")
    public AjaxResult getChildCategories(@PathVariable("parentId") Long parentId) {
        List<AidCategory> list = aidCategoryService.selectAidCategoryByParentId(parentId);
        return success(list);
    }
} 